﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;

namespace LEDScreenLinkTool.Model
{
    public class ConnectLineDrawing : UIElement
    {

        private readonly VisualCollection _visuals;
        public VisualCollection Visuals
        {
            get => _visuals;
        }

        protected override int VisualChildrenCount => Visuals.Count;

        // Provide a required override for the GetVisualChild method.
        protected override Visual GetVisualChild(int index)
        {
            if (index < 0 || index >= Visuals.Count)
            {
                throw new ArgumentOutOfRangeException();
            }

            return Visuals[index];
        }

        private int circleRadius = 7;

        private List<SolutionLine> _solutionLinks;


        public ConnectLineDrawing()
        {
            _visuals = new VisualCollection(this);
            _solutionLinks = new List<SolutionLine>();
            IsHitTestVisible = false;

        }

        public void SetLinkLine(List<SolutionLine> linkPoints)
        {
            _solutionLinks = linkPoints;

        }

        public void ClearPoints()
        {
            _solutionLinks.Clear();
            Visuals.Clear();
        }

        /// <summary>
        /// 刷新连线
        /// </summary>
        public void RefreshLine(int lineIndex)
        {
            Visuals.Clear();
            if (_solutionLinks.Count < 1 || lineIndex>= _solutionLinks.Count) return;
            DrawingVisual drawingVisual = new DrawingVisual();
            Pen linePencil = new Pen();
            linePencil.Thickness = 1.5;
            linePencil.Brush = new SolidColorBrush(Color.FromRgb(0xFF, 0x55, 0x00));
            var beginBrush = new SolidColorBrush(Color.FromRgb(0x00, 0xA0, 0x00));
            var endBrush = new SolidColorBrush(Color.FromRgb(0xFF, 0x00, 0x00));
            using (DrawingContext dc = drawingVisual.RenderOpen())
            {
                double x1 = 0;
                double y1 = 0;
                double x2 = 0;
                double y2 = 0;

                var pointList = _solutionLinks[lineIndex].Points;
                for (int i = 0; i < pointList.Count; i++)
                {
                    x2 = pointList[i].X;
                    y2 = pointList[i].Y;
                    if (i > 0)
                    {
                        x1 = pointList[i - 1].X;
                        y1 = pointList[i - 1].Y;
                        Point beginPoint = new Point(x1, y1);
                        Point endPoint = new Point(x2, y2);
                        dc.DrawLine(linePencil, beginPoint, endPoint);
                    }
                    if (i == 0)
                    {
                        dc.DrawEllipse(beginBrush, linePencil, new Point(x2, y2), circleRadius, circleRadius);
                    }
                    else if (i == pointList.Count - 1)
                    {
                        dc.DrawEllipse(endBrush, linePencil, new Point(x2, y2), circleRadius, circleRadius);
                    }
                    else
                    {
                        dc.DrawEllipse(linePencil.Brush, linePencil, new Point(x2, y2), circleRadius, circleRadius);
                    }
                }
            }
            Visuals.Add(drawingVisual);
        }

    }
}
